<?php
/**
 * polwebgis
 * 
 * This file is part of polwebgis.
 *
 * @copyright   Copyright (c) 2012 Dominik Janisch (http://www.joanneum.at)
 * @lastmodified    $LastChangedDate: 2012-03-29 13:55:30 +0000 (Thu, 29 Mar 2012) $
 * @revision    $Revision: 171 $ 
 */

require_once(PATH_TO_APP . "/components/outputhandler/dwoo/dwooAutoload.php");

class OutputHandler
{

    private $status_ = "200 OK";
    private $template_system_;
    private $template_system_data_;
    private $header_shown_ = false;
    private $arr_headers_ = array();
    private $template_path_;
    private static $class_instance_;
    private static $instance_ = false;

    protected function __construct($theme)
    {
        $this->template_system_ = new Dwoo();
        $this->template_system_data_ = new Dwoo_Data();
        $this->template_path_ = PATH_TO_APP . "/output/theme/". $theme ."/templates";
    }
        
    public function __destruct(){}
    
    private function __clone(){}
    
    public static function getInstance($theme)
    {
        if(!self::$instance_)
        {
            self::$class_instance_ = new OutputHandler($theme);
            self::$instance_ = true;
            return self::$class_instance_;
        }
        return self::$class_instance_;
    }
    
    public function setStatus($status)
    {
        $this->status_ = $status;
    }
    
    public function addHeader($name, $value)
    {
        $this->arr_headers_[$name] = $value;
    }
    
    public function assignValuesToTemplate($arr_variables)
    {
        try{
            foreach($arr_variables as $key => $value)
                $this->template_system_data_->assign($key, $value);
        }catch(Dwoo_Exception $e)
        {
            var_dump($e);
        }
    }
    
    public function showTemplate($template_name)
    {
        if(!$this->header_shown_)
            $this->showHeaders();
        $tpl = $this->template_path_ . '/'. $template_name . '.tpl';      
        $this->template_system_->output($tpl, $this->template_system_data_);
    }
        
    public function showHeaders()
    {
        //header('Content-type: text/html; charset=utf-8');
        header("HTTP/1.1 {$this->status_}");
        foreach($this->arr_headers_ as  $name => $value)
            header("{$name}: {$value}");
        $this->header_shown_ = true;
    }
    
    public function showJSON($arr_values)
    {
        $this->addHeader('Content-Type', 'application/json');
        $this->showHeaders();
        echo json_encode($arr_values);
    }

}

?>
